package com.zzb.framework.frameworkcontroller.mail;

import com.zzb.framework.common.*;
import com.zzb.framework.mail.entity.Mail;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.mail.form.MailSearchForm;
import com.zzb.framework.mail.service.MailService;
import com.zzb.framework.frameworkcontroller.common.FrameworkBaseController;
import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 邮件controller
*/
@Controller
@RequestMapping("/framework/mail")
public class FrameworkMailController extends FrameworkBaseController {
    private static final Logger logger = LoggerFactory.getLogger(FrameworkMailController.class);
    @Autowired
    private MailService mailService;

    /**邮件管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "framework/mail/mail/index";
    }

    /**邮件列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<Mail> list(MailSearchForm mailSearchForm){
        return mailService.queryPage(mailSearchForm);
    }

    /**邮件表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        Mail mail;
        if(StringUtils.isEmpty(id)){
            mail = new Mail();
        } else{
            mail = mailService.get(id);
            if(mail == null) mail = new Mail();
        }
        model.addAttribute("mail",mail);
        return "framework/mail/mail/form";
    }

    /**邮件保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<Mail> save(Mail mail , HttpServletRequest request){
        try {
            IUser user = getCurrentUser(request);
            return mailService.saveMail(mail, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**邮件删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return mailService.deleteMail(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**邮件开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return mailService.recoveryById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**邮件屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return mailService.trashById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
